package com.ywj.client;

import javax.websocket.*;
import java.io.IOException;

/**
 * 开发客户端
 */
@ClientEndpoint
public class MyClientEndpoint {
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to endpoint: " + session.getBasicRemote());
        try {
//            Client.session=session;
            System.out.println("当前会话为:   " + Client.session);
            String name = "Duke";
            System.out.println("Sending message to endpoint: " + name);
            session.getBasicRemote().sendText(name);  //向服务端发消息
            System.out.println("当前会话为:   " + Client.session);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @OnMessage     //服务端发来消息后的处理方法
    public void processMessage(String message) {
        //对方发来的消息，进行处理
        System.out.println("Received message in client: " + message);
        System.out.println("服务端发来消息，当前session    " + Client.session);
        System.out.println("服务端发来消息，当前session.id   " + Client.session.getId());

    }

    @OnError
    public void processError(Throwable t) {
        t.printStackTrace();
    }

    @OnClose
    public void onClose(){
        System.out.println("与服务端端开连接");
    }
}